package com.cmc.cloud.cmclink.doc.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.DgCommodityErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.DgCommodityPsnConvert;
import com.cmc.cloud.cmclink.doc.entity.DgCommodityPsnDO;
import com.cmc.cloud.cmclink.doc.mapper.DgCommodityPsnMapper;
import com.cmc.cloud.cmclink.doc.service.DgCommodityPsnService;
import com.cmc.cloud.cmclink.doc.vo.special.dgcommoditypsnvo.DgCommodityPsnCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.special.dgcommoditypsnvo.DgCommodityPsnUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;

/**
 * 危险品品名子表库 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class DgCommodityPsnServiceImpl implements DgCommodityPsnService {

    @Resource
    private DgCommodityPsnMapper dgCommodityPsnMapper;

    @Override
    public void createDgCommodityPsn(DgCommodityPsnCreateReqVO createReqVO) {
        // 插入
        DgCommodityPsnDO dgCommodityPsn = DgCommodityPsnConvert.INSTANCE.convert(createReqVO);
        dgCommodityPsnMapper.insert(dgCommodityPsn);
    }

    @Override
    public void updateDgCommodityPsn(DgCommodityPsnUpdateReqVO updateReqVO) {
        // 校验存在
        validateDgCommodityPsnExists(updateReqVO.getId());
        // 更新
        DgCommodityPsnDO updateObj = DgCommodityPsnConvert.INSTANCE.convert(updateReqVO);
        dgCommodityPsnMapper.updateById(updateObj);
    }

    @Override
    public void deleteDgCommodityPsn(Long id) {
        // 校验存在
        validateDgCommodityPsnExists(id);
        // 删除
        dgCommodityPsnMapper.deleteById(id);
    }

    private void validateDgCommodityPsnExists(Long id) {
        if (dgCommodityPsnMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(DgCommodityErrorCodeConstants.DG_COMMODITY_PSN_LIMITED_EXISTED);
        }
    }

    @Override
    public DgCommodityPsnDO getDgCommodityPsn(Long id) {
        return dgCommodityPsnMapper.selectById(id);
    }

    @Override
    public List<DgCommodityPsnDO> getDgCommodityPsnsByMainId(Long mainId) {
        LambdaQueryWrapper<DgCommodityPsnDO> queryWrapper =
            Wrappers.<DgCommodityPsnDO>lambdaQuery().eq(DgCommodityPsnDO::getMainId, mainId);
        return dgCommodityPsnMapper.selectList(queryWrapper);
    }

}
